<?php
namespace app\admin\controller;
use app\model\Config as ConfigModel;
use think\Db;


class Config extends Base
{
    public function __construct(){
        $model = new ConfigModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '系统设置';

        if ( in_array($page_info['action'], ['add','edit']) ) {
            $fields = [
                'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
                'code'=>['name'=>'编号','type'=>'text','valids'=>['required']]
            ];
            $valids = createPageValid($fields);
            $this->assign([
                'fields'=>$fields,
                'valids'=>$valids
            ]);
        }

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function index(){
        $model = $this->model;
        if (request()->isPost()) {
            $param = request()->param();    
            // 过滤掉上传组件的file
            if (isset($param['file']) && empty($param['file'])) {
                unset($param['file']);
            }
            foreach ($param as $k => $vo) {
                $model->where('key',$k)->update(['value'=>$vo]);
            }
            $this->success('修改成功');
        }
        // $list = $model->where('group',1)->select();
        $list = $model->select();
        $fields = [];
        foreach ($list as $k => $vo) {
            $fields[$vo['key']] = [
                'name'=>$vo['name'],
                'type'=>$vo['type'],
                'value'=>$vo['value'],
                'placeholder'=>$vo['remark']
            ];
        }
        return view('index', [
            'fields'=>$fields
        ]);
    }
   
}
